/**
 * @file skoll.actions.content.rules.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: skoll.actions.content.rules.js 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.content.rules.js $
 */

Skoll.actions.content.rules = {
	//
	// Protected properties.
	//
	// Public methods.
	createRule: function(form) {
		Skoll.tools.Loading.start(0);
		Skoll.tools.preventDefault();

		var nameInput = $(form).find("#NewRuleName");
		var name = nameInput.val().trim();

		if (name.length > 0) {
			$.ajax({
				type: "POST",
				url: "service.php?service=rules:add:rule&group=" + GROUP_ID,
				data: {
					params: JSON.stringify({
						"name": name
					})
				},
				dataType: "json",
				success: function(data) {
					if (data.status) {
						nameInput.val("");
						window.location.href = "?action=content:rule&group=" + GROUP_ID + "&rule=" + data.data.id + Skoll.tools.extraUrlParams();
					} else {
						Skoll.tools.log("Error: [" + data.error.code + "] " + data.error.message);
						alert("Failed: " + data.error.message);
						Skoll.tools.Loading.stop();
					}
					nameInput.focus();
				},
				error: function(data) {
					alert(data);
					Skoll.tools.Loading.stop();
				}
			});
		} else {
			nameInput.focus();
			alert("You must enter a rule name");
			Skoll.tools.Loading.stop();
		}
	},
	removeRule: function(ruleId, ruleName) {
		Skoll.tools.preventDefault();
		Skoll.tools.ConfirmWindow.openSimpleConfirm(
			"Are you sure you want to remove rule '" + ruleName + "'?",
			function() {
				Skoll.tools.Loading.start(0);
				$.ajax({
					type: "POST",
					url: "service.php?service=rules:remove:rule&group=" + GROUP_ID + "&rule=" + ruleId,
					data: {
						params: JSON.stringify({})
					},
					dataType: "json",
					success: function(data) {
						if (data.status) {
							Skoll.tools.reload();
						} else {
							Skoll.tools.log("Error: [" + data.error.code + "] " + data.error.message);
							alert("Failed: " + data.error.message);
							Skoll.tools.Loading.stop();
						}
						nameInput.focus();
					},
					error: function(data) {
						alert(data);
						Skoll.tools.Loading.stop();
					}
				});
			}
		);
	}
	//
	// Protected methods.
};